<?php
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/class/abc/PFTCoreAPI.class.php';
include_once '/var/www/html/new/d/common/area.inc.php';
include_once '/var/www/html/new/d/class/Member.php';

if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"请先登录后再操作","code":0}');

if(!isset($_REQUEST['city'])) $_REQUEST['city'] = '';
if(!isset($_REQUEST['topic'])) $_REQUEST['topic'] = '';
if(!isset($_REQUEST['provice'])) $_REQUEST['provice'] = '';
if(!isset($_REQUEST['pageSize'])) $_REQUEST['pageSize'] = 5;
if(!isset($_REQUEST['currentPage'])) $_REQUEST['currentPage'] = 1;

// 分页依据
$city = $_REQUEST['city'];$provice  = $_REQUEST['provice']; $pageSize = $_REQUEST['pageSize']+0;
$currentPage = $_REQUEST['currentPage']+0; 
if($pageSize<1) $pageSize = 5;
if($currentPage<1) $currentPage = 1;
$pStart = ($currentPage-1) * $pageSize;

// 景区模糊查询
$lids = array();
if(isset($_REQUEST['ltitle']) && $_REQUEST['ltitle']){
	$ltitle = trim($_REQUEST['ltitle']);
	$sql = "select id from uu_land where status=1 and title like '%$ltitle%'";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $lids[$row['id']] = $row['id'];
} 
$lids_count = count($lids);// 模糊查询出来的景区ID
$lands = $condition = $sales = array();
$response_a = array('status'=>'success','currentPage'=>$currentPage,'data'=>array(), 'timer'=>time());


$memberID   =$_SESSION['sid'];
$m          = new Member($s,$memberID);
$salePros   = $m->salePros();

foreach($salePros as $key=>$row){
	$row['tuijian'] = $row['px'];
	if($row['sapply_did']==$_SESSION['sid']) $row['tuijian'] += $row['px'];
	$salePros_a[$key] = $row;
}
$salePros = $salePros_a;
if(count($salePros)==0)  goto EEEEEnd;

uasort($salePros,  function($a,$b){
	$tuijiana = $a['tuijian'];
	$tuijianb = $b['tuijian'];
	if($tuijiana==$tuijianb){
		$pxa = $a['px'];
		$pxb = $b['px'];
		if($pxa==$pxb) return 0;
		return ($pxa>$pxb) ? -1 : 1;
	}
	return ($tuijiana>$tuijianb) ? -1 : 1;
});
	
	
// if($lids_count==1) $condition['lid'] = current($lids);
// if(is_numeric(trim($_REQUEST['ltitle']))) $condition['tid'] = trim($_REQUEST['ltitle']);
// $condition['fields'] = array('l.address','t.pay', 't.tprice','t.getaddr','l.imgpath','l.area','l.topic','l.p_type','t.delaydays','t.order_end','t.order_start','t.delaytype');
// $salePros = PFTCoreAPI::salePros($condition);


// 获取供应商信息
$suppliers = array(); $memberSID = $_SESSION['sid'];
$sql = "select r.id,r.parent_id,m.dname,m.qq from pft_member_relationship r left join pft_member m on r.parent_id=m.id where r.son_id=$memberSID and r.ship_type=0 and r.status=0 and parent_id<>$memberSID and m.status=0 and m.id>1";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()) $suppliers[$row['parent_id']] = $row;
$sql = "select id as parent_id,dname,qq from pft_member where id=$memberSID limit 1";
$GLOBALS['le']->query($sql);
$suppliers[$memberSID] = $GLOBALS['le']->fetch_assoc();


// 按销售渠道配置排序
// $channel_a = array();
// $sql = "select lid,pid,px from pft_sale_channel where fid=$memberSID";
// $GLOBALS['le']->query($sql);
// while($row = $GLOBALS['le']->fetch_assoc()) $channel_a[$row['lid']] = $row['px'];
// if(count($channel_a)){
	// $salePros_a = array();
	// foreach($salePros as $key=>$row){
		// $row['px'] = 0;
		// if($row['sapply_did']==$_SESSION['sid']) $row['px']+=65535;
		// if(isset($channel_a[$row['lid']])) $row['px']+= $channel_a[$row['lid']];
		// $salePros_a[$key] = $row;
	// }
	// $salePros = $salePros_a;
	// uasort($salePros,  function($a,$b){
		// $tuijiana = $a['px'];
		// $tuijianb = $b['px'];
		// if($tuijiana==$tuijianb) return 0;
		// return ($tuijiana>$tuijianb) ? -1 : 1;
	// });
// }



// 按景区重组数组
foreach($salePros as $row){

	if(!isset($suppliers[$row['sapply_did']])) continue;
	if($lids_count && !isset($lids[$row['lid']])) continue;
	if($_REQUEST['topic']!='' && $row['topic']=='') continue;
	if($_REQUEST['ptype'] && $row['p_type']!=$_REQUEST['ptype']) continue;
	if(($_REQUEST['provice'] || $_REQUEST['city']) && $row['area']=='') continue;

	$assKey = $row['lid'].'_'.$row['sapply_did'];
	
	if($row['topic']!=''){
		$topic_a = explode(',', $row['topic']);
		if($_REQUEST['topic'] && !in_array($_REQUEST['topic'], $topic_a)) continue;
		if(!isset($lands[$assKey])) foreach($topic_a as $topic) $themes[$topic] += 1;
	}
	
	if($row['area']!=''){
		$areas_split = explode('|', $row['area']);// 拆分地区
		if($provice && !in_array($provice, $areas_split)) continue;
		if($city && !in_array($city, $areas_split)) continue;
		if(isset($areas[$areas_split[0]])){
			$row['province'] = $areas[$areas_split[0]];
			$row['city'] = $areas[$areas_split[1]];
			if(!isset($lands[$assKey])){
				if(!isset($provices[$areas_split[0]])) $provices[$areas_split[0]] = array('name' => $row['province']);
				$provices[$areas_split[0]]['citys'][$areas_split[1]] = array('name' => $row['city']);	
			}
		}
	}
	$lands[$assKey][] = $row;
}


if(count($lands)==0) goto EEEEEnd;

$ac      = $GLOBALS['_ac'];
$pw      = $GLOBALS['_pw'];
$inside  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

// 价格缓存
$cache_file = "/var/www/html/new/d/logs/cache_logs/SalePros/Price_cache_{$_SESSION['sid']}.php";
$pStorage_a = file_exists($cache_file) ? include $cache_file:array();

$count = 0;
$point = 0;
$today = date('Y-m-d');
$time  = time();
$update= false;
foreach($lands as $lid_aid=>$tickets){
	if(($point++)<$pStart) continue;
	if(($count++)>=$pageSize) break;
	foreach($tickets as $ticket){
		
		$reGetPrice = true;
		$assKey = $ticket['tid'].'_'.$ticket['sapply_did'];
		if(isset($pStorage_a[$assKey]) && ($pStorage_a[$assKey]['time']+1800>$time)){
			$pStorage = $pStorage_a[$assKey];
			if($pStorage['js']['p']>-1) $reGetPrice = false;
		}
		if($reGetPrice)
		{
			$pStorage = PFTCoreAPI::pStorage($inside, $_SESSION['saccount'], $ticket['pid'], $ticket['sapply_did'], $today);
			$pStorage['time'] = $time;
			$pStorage_a[$assKey] = $pStorage;
			$update = true;
		}
		if($pStorage['js']['p']<0) continue;
		
		if(!isset($sales[$lid_aid])){// 景区信息	
			
			$land['lid']   = $ticket['lid'];
			$land['city']  = $ticket['city'];
			$land['title'] = $ticket['title'];
			$land['aid']   = $ticket['sapply_did'];
			$land['qq']    = $suppliers[$ticket['sapply_did']]['qq'];
			$land['dname'] = $suppliers[$ticket['sapply_did']]['dname'];
			$land['address'] = $ticket['address'];
			$land['imgpath'] = $ticket['imgpath'];
			$sales[$lid_aid]   = $land;
		}
		$ticket['ms']  = $ticket['tprice'];
		$ticket['ls']  = $pStorage['ls']['p'];
		$ticket['js']  = $pStorage['js']['p'];
		$ticket['aid'] = $ticket['sapply_did'];				
		$ticket['usetime'] = htmlValid($ticket);
		$ticket['explain'] = $ticket['notes'];
		$sales[$lid_aid]['ticket'][$ticket['tid']]= $ticket;
	}

}

$response_a = array('status'=>'success','currentPage'=>$currentPage,'lands'=>$sales, 'timer'=>time());
if($currentPage==1){
	$response_a['provices'] = $provices;
	$response_a['citys']    = $citys;
	$response_a['themes']   = $themes;
}


$cache_text = "<?php\nreturn ".var_export($pStorage_a, true).';';
if($update) file_put_contents($cache_file, $cache_text);


EEEEEnd:
$GLOBALS['le']->close();
exit(json_encode($response_a));



function htmlValid($p_info){
	$valid = '';
	if($p_info['delaydays']>0){
		if($p_info['delaytype']==0) $valid = '游玩日期(含)'.$p_info['delaydays'].'天内有效';
		if($p_info['delaytype']==1) $valid = '下单日期(含)'.$p_info['delaydays'].'天内有效';
	}
	if($p_info['order_start']=='' && $p_info['delaydays'] == 0) $valid = '订单当天有效';
	
	if($p_info['order_start']!='' && $p_info['order_end']!='' ){
		$valid = date('Y-m-d', strtotime($p_info['order_start'])).' 至 '.date('Y-m-d', strtotime($p_info['order_end']));
	}elseif($p_info['order_start']=='' && $p_info['order_end']!=''){
		$valid = '截止'.$p_info['order_end'];
	}
	return $valid;
}

// 调试日志
function write_log($txt, $suffix=''){
	$file = '/var/www/html/new/d/abc/logs/plist_'.$_SESSION['sid'].'.txt';
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}
